package back.services;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import config.GeneralConfig;

import utils.helpers.logging.LineLog;

public class Ping {
	public static boolean execute(String hostname) {
		int count = 0;
		String str = "";
		String param1 = "-c";	// linux
		
		LineLog linelog = new LineLog (  );
		Process process;
		
		if ( GeneralConfig.OS == "win" )
			param1 = "-n";
			
		try {
			process = new ProcessBuilder()
					.command("ping", param1, "2", hostname)
					.redirectErrorStream(true).start();
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					process.getInputStream()));

			StringBuffer output = new StringBuffer();
			String temp;

			while ((temp = reader.readLine()) != null)// .read(buffer)) > 0)
			{
				output.append(temp);
				count++;
			}

			reader.close();

			if (count > 0)
				str = output.toString();

			process.destroy();
			linelog.addpair("OK", Integer.toString(   process.exitValue() ) );
			linelog.addpair("RESULT", str );
			linelog.out();
			if ( process.exitValue() == 0 )
				return true;
			else 
				return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

}
